package com.wangzhenyu.learn.test.designPattern


//迭代器模式
@Suppress("UNUSED")
object QIteratorPattern {


    data class Department(var name: String, var desc: String)

    class ComputerCollegeIterator(private val departments: List<Department>) :
        Iterator<Department> {

        private var position: Int = -1

        override fun hasNext(): Boolean {
            return if (position < departments.size - 1) {
                position += 1
                true
            } else
                false

        }

        override fun next(): Department {
            return departments[position]
        }
    }


    class InfoCollegeIterator(private val departments: List<Department>) : Iterator<Department> {

        private var position: Int = -1

        override fun hasNext(): Boolean {
            return if (position < departments.size - 1) {
                position += 1
                true
            } else
                false

        }

        override fun next(): Department {
            return departments[position]
        }
    }

    interface College {

        fun getName(): String

        fun addDepartment(name: String, desc: String)

        fun createIterator(): Iterator<Department>
    }

    class ComputerCollege : College {

        private val departments = mutableListOf(
            Department("Java 开发", "Java 开发"),
            Department("IOS 开发", "IOS 开发"),
            Department("Android 开发", "Android 开发")
        )
        private var numOfDepartment = 0

        override fun getName() = "计算机学院"

        override fun addDepartment(name: String, desc: String) {
            departments[numOfDepartment] = Department(name, desc)
            numOfDepartment += 1
        }

        override fun createIterator(): Iterator<Department> {
            return ComputerCollegeIterator(departments)
        }
    }

    class InfoCollege : College {

        private val departments = mutableListOf(
            Department("信息安全专业", "信息安全专业"),
            Department("网络安全专业", "网络安全专业"),
            Department("服务器安全专业", "服务器安全专业")
        )
        private var numOfDepartment = 0

        override fun getName() = "计算机学院"

        override fun addDepartment(name: String, desc: String) {
            departments[numOfDepartment] = Department(name, desc)
            numOfDepartment += 1
        }

        override fun createIterator(): Iterator<Department> {
            return InfoCollegeIterator(departments)
        }
    }

    class OutputImpl(private val collegeList: List<College>) {

        fun printlnCollage() {
            val iterator = collegeList.iterator()

            while (iterator.hasNext()) {
                val college = iterator.next()
                println(college.getName())
                printDepartment(college.createIterator())
            }
        }

        private fun printDepartment(iterator: Iterator<Department>) {
            while (iterator.hasNext()) {
                println(iterator.next())
            }

        }

    }
}